package kr.hkit.androidcomplete02.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01.*;
import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class ListViewAddDelMultipleEx extends Activity {
	ArrayList<String> items;
	ArrayAdapter<String> adapter;
	ListView listView;
	EditText et;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listviewadddel);
	
		et = (EditText) findViewById(R.id.ch12_edit_newitem);
			
		items = new ArrayList<String>();
		items.add("First");
		items.add("Second");
		items.add("Third");
		items.add("Fourth");
		items.add("Fifth");
		
		//어댑터 준비
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items);
				
		//ListView
		listView = (ListView) findViewById(R.id.ch12_listviewadddel);
		listView.setAdapter(adapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
	}
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.ch12_btn_add:
			String text = et.getText().toString();
			items.add(text);
			et.setText("");
			adapter.notifyDataSetChanged();
			break;
		
		case R.id.ch12_btn_del:
			SparseBooleanArray sb = listView.getCheckedItemPositions();
			if(sb.size() != 0){
				for(int i = listView.getCount() - 1; i > -1; i--){
					if(sb.get(i)){
						items.remove(i);
					}
				}
				listView.clearChoices();
				adapter.notifyDataSetChanged();
			}
		
			break;	
		}
	}
	
	
}
